package com.hermes.test;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.hermes.biz.common.model.result.UserResultDO;
import com.hermes.biz.common.model.user.UserDO;
import com.hermes.biz.manager.user.UserOperateManager;
import com.hermes.common.action.AsynBaseAction;
import com.hermes.common.util.ParameterParserUtil;
import com.opensymphony.xwork2.ActionSupport;

public class ChangePsw extends ActionSupport{
	public UserOperateManager userOperateManager;
	public UserDO userDO;
	
	@Override
	public String execute() throws Exception {
		//params
		String nowpsw = ParameterParserUtil.getParameter("nowpsw");
		String newpsw = ParameterParserUtil.getParameter("newpsw");
		String newpsw2 = ParameterParserUtil.getParameter("newpsw2");
		String userid = ParameterParserUtil.getParameter("userid");
		//logic
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");				
		
		
		userDO = new UserDO();
		if(null!=userid){
			userDO.setUserId(Long.valueOf(userid));			
			if(null!=userDO){
				UserResultDO resultDO = userOperateManager.getSingleUserByUserId(Long.valueOf(userid));
				userDO = resultDO.getModule();
				if(userDO.getUserPassword().equals(nowpsw)){//now password correct
					userDO.setUserPassword(newpsw);
					userDO.setUserId(Long.valueOf(userid));
					userOperateManager.updateUserPassword(userDO);//update password
					response.getWriter().print("ok"); 
				}else{
					response.getWriter().print("not"); 
				}
			}
		}
		else{
			//log error user id null
		}
		
		response.getWriter().flush();
		response.getWriter().close();
		return SUCCESS;
	}

	//getters and setters
	public UserOperateManager getUserOperateManager() {
		return userOperateManager;
	}

	public void setUserOperateManager(UserOperateManager userOperateManager) {
		this.userOperateManager = userOperateManager;
	}

}
